#!/bin/sh

compDose()
{
# plan directory
cpwd=`pwd`
d="./$1"

if [ ! -e "${d}" ]; then
    echo "${d} directory does not exist"
    exit 1
fi

if [ ! -e "${d}/a" ]; then
    echo "${d}/a directory does not exist"
    exit 1
fi

if [ ! -e "${d}/b" ]; then
    echo "${d}/b directory does not exist"
    exit 1
fi

if [ ! -e "${d}/dvh" ]; then
    echo "${d}/dvh directory does not exist"
    exit 1
fi

echo "Creating list of anastructures"
anaList=`for i in \`ls ${d}/a\`; do echo "${i}"; done`

# Check if all dose matrices exist and if not create it
echo "Checking if all dose matrices are created"
beamList=`ls ${d}/b`
gList=""
for i in ${beamList}; do
  gList="${gList} -g ${d}/g/${i}"
  if [ ! -e "${d}/g/${i}" ]; then
    bn=`print_beam_name -b ${d}/b/${i}`
    echo "need to calc ${d}/g/${i} ${bn}"
    rm -f ${d}/sum
    echo "c_photon -a ${d}/a/skin -b ${d}/b/${i} -g ${d}/grid -o ${d}/g/${i}"
    c_photon -a ${d}/a/skin -b ${d}/b/${i} -g ${d}/grid -o ${d}/g/${i}
  fi
done

# Sum all grid files to one sum file
echo "Sum grid files from g directory to sum file"
echo "sum_grids_sep -w ${d}/wt -o ${d}/sum ${gList}"
sum_grids_sep -w ${d}/wt -o ${d}/sum ${gList}

cd ${cpwd}
}
